﻿﻿using System;
using System.Windows.Forms;
using System.Drawing;

struct savedColor
{
    public Color color;
    public int red;
    public int green;
    public int blue;
    public int presetIndex;
    public void SetColor(Color saved)
    {
        color = saved;
        red = (int)color.R;
        green = (int)color.G;
        blue = (int)color.B;
    }
};

namespace Color_Chooser
{
    public partial class MainForm : Form
    {
        bool preset = false;
        bool colorSet = false;
        bool fgSet = false;
        bool bgSet = false;
        savedColor fgColor;
        savedColor bgColor;
        string filename = "";


        Color[] colors = { Color.AntiqueWhite, Color.Aqua, Color.Azure, Color.Beige, 
                             Color.Bisque, Color.Black, Color.Blue, Color.BlueViolet, 
                             Color.Brown, Color.BurlyWood, Color.Chartreuse, Color.Chocolate,
                             Color.Coral, Color.CornflowerBlue, Color.Crimson, Color.Cyan, 
                             Color.DarkBlue, Color.DarkGray, Color.DarkGreen, Color.Firebrick, 
                             Color.FloralWhite, Color.ForestGreen, Color.Fuchsia, Color.Gold, 
                             Color.Goldenrod, Color.Gray, Color.Green, Color.GreenYellow, 
                             Color.HotPink, Color.Indigo, Color.Khaki, Color.Lavender, 
                             Color.LightBlue, Color.LightGray, Color.LightGreen, Color.Lime, 
                             Color.Magenta, Color.Maroon, Color.MidnightBlue, Color.MintCream, 
                             Color.MistyRose, Color.Navy, Color.OldLace, Color.Olive, 
                             Color.OliveDrab, Color.Orange, Color.OrangeRed, Color.Orchid, 
                             Color.Pink, Color.Plum, Color.PowderBlue, Color.Purple, 
                             Color.RoyalBlue, Color.SaddleBrown, Color.Salmon, Color.SeaGreen, 
                             Color.SeaShell, Color.Silver, Color.SkyBlue, Color.SlateBlue, 
                             Color.SlateGray, Color.SteelBlue, Color.Tan, Color.Teal, 
                             Color.Tomato, Color.Turquoise, Color.Violet, Color.Wheat, 
                             Color.White, Color.Yellow, Color.YellowGreen, Color.Black 
                         };

        public MainForm()
        {
            InitializeComponent();
            toolTip1.SetToolTip(bgLbl, "Click to show larger preview");
            toolTip1.SetToolTip(colorBtn, "Click to choose a color");
            toolTip1.SetToolTip(setBtn, "Click to set a color");
            toolTip1.SetToolTip(button2, "Click to convert RGB values to Hex");
            toolTip1.SetToolTip(comboBox1, "Select a preset color here");
            toolTip1.SetToolTip(bgRadio, "Click here to select the background color");
            toolTip1.SetToolTip(fgRadio, "Click here to set the foreground color");
            fgColor.SetColor(Color.Black);
            bgColor.SetColor(Color.White);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                int r;
                int b;
                int g;
                Color myColor;

                myColor = colorDialog1.Color;
                r = (int)myColor.R;
                g = (int)myColor.G;
                b = (int)myColor.B;
                colorBtn.BackColor = myColor;
                textBox1.Text = "#" + myColor.ToArgb().ToString("X8").Substring(2, 6);
                redText.Text = r.ToString();
                greenText.Text = g.ToString();
                blueText.Text = b.ToString();
                colorSet = true;
                colorDialog1.Dispose();
                preset = false;
                if (comboBox1.SelectedIndex >= 0)
                    comboBox1.SelectedIndex = -1;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!validateNumbers(redText.Text) || !validateNumbers(greenText.Text) || !validateNumbers(blueText.Text))
            {
                MessageBox.Show("Please enter integers between 0 and 255", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            else
            {
                int r = int.Parse(redText.Text);
                int g = int.Parse(greenText.Text);
                int b = int.Parse(blueText.Text);
                Color myColor = Color.FromArgb(r, g, b);
                textBox1.Text = "#" + myColor.ToArgb().ToString("X8").Substring(2, 6);
                colorBtn.BackColor = myColor;
                colorDialog1.Color = myColor;
                colorSet = true;
                preset = false;
                if (comboBox1.SelectedIndex >= 0)
                    comboBox1.SelectedIndex = -1;
            }
        }

        private bool validateNumbers(String myText)
        {
            try
            {
                if (myText.Length == 0)
                    return false;
                int n = int.Parse(myText);
                if (n < 0 || n > 255)
                    return false;
                return true;
            }
            catch (System.FormatException)
            {
                return false;
            }
        }

        private void setBtn_Click(object sender, EventArgs e)
        {
            if (fgRadio.Checked == true && colorSet)
            {
                bgLbl.ForeColor = colorDialog1.Color;
                fgColor.SetColor(colorDialog1.Color);
                fgSet = true;
                if (preset)
                {
                    fgColor.presetIndex = comboBox1.SelectedIndex;
                }
                else
                    fgColor.presetIndex = -1;
            }
            else if (bgRadio.Checked == true && colorSet)
            {
                bgLbl.BackColor = colorDialog1.Color;
                bgColor.SetColor(colorDialog1.Color);
                bgSet = true;
                if (preset)
                {
                    bgColor.presetIndex = comboBox1.SelectedIndex;
                }
                else
                    bgColor.presetIndex = -1;
            }
            else
            {
                MessageBox.Show("Please select a color first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            if (fgSet && bgSet)
                menuItem2.Enabled = true;

        }

        private void showTextPanelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (splitContainer1.Panel2Collapsed == true)
            {
                splitContainer1.Panel2Collapsed = false;
                menuItem8.Text = "Hide preview panel";
                MainForm.ActiveForm.Height = 275;
            }
            else
            {
                splitContainer1.Panel2Collapsed = true;
                menuItem8.Text = "Show preview panel";
                MainForm.ActiveForm.Height = 198;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            splitContainer1.Dispose();
            Application.Exit();
        }

        private void bgRadio_Click(object sender, EventArgs e)
        {
            if (bgRadio.Checked == false && fgSet && bgSet)
            {
                colorDialog1.Color = fgColor.color;
                colorBtn.BackColor = fgColor.color;
                redText.Text = fgColor.red.ToString();
                greenText.Text = fgColor.green.ToString();
                blueText.Text = fgColor.blue.ToString();
                textBox1.Text = "#" + fgColor.color.ToArgb().ToString("X8").Substring(2, 6);
                if (fgColor.presetIndex > 0)
                {
                    comboBox1.SelectedIndex = fgColor.presetIndex;
                }
                else
                    comboBox1.SelectedIndex = -1;
            }
        }

        private void fgRadio_Click(object sender, EventArgs e)
        {
            if (fgRadio.Checked == false && fgSet && bgSet)
            {
                colorDialog1.Color = bgColor.color;
                colorBtn.BackColor = bgColor.color;
                redText.Text = bgColor.red.ToString();
                greenText.Text = bgColor.green.ToString();
                blueText.Text = bgColor.blue.ToString();
                textBox1.Text = "#" + bgColor.color.ToArgb().ToString("X8").Substring(2, 6);
                if (bgColor.presetIndex > 0)
                {
                    comboBox1.SelectedIndex = bgColor.presetIndex;
                }
                else
                    comboBox1.SelectedIndex = -1;
            }
        }

        private void bgLbl_Click(object sender, EventArgs e)
        {
            PreviewBox otherFrm = new PreviewBox();
            otherFrm.splitContainer1.Panel2.BackColor = bgColor.color;
            otherFrm.label1.ForeColor = fgColor.color;
            otherFrm.label1.BackColor = bgColor.color;
            otherFrm.label2.ForeColor = fgColor.color;
            otherFrm.label2.BackColor = bgColor.color;
            otherFrm.bgLbl.Text = "#" + bgColor.color.ToArgb().ToString("X8").Substring(2, 6);
            otherFrm.fgLbl.Text = "#" + fgColor.color.ToArgb().ToString("X8").Substring(2, 6);
            otherFrm.splitContainer1.Panel2Collapsed = false;
            otherFrm.ShowDialog();
            otherFrm.Dispose();
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.SelectAll();
            textBox1.Copy();
            textBox1.DeselectAll();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox myForm = new AboutBox();
            myForm.progBox.Text = Application.ProductName;
            myForm.verBox.Text = ProductVersion;
            myForm.nameBox.Text = "David Baker";
            myForm.companyBox.Text = Application.CompanyName;
            myForm.iconBox.Image = Properties.Resources.icon_small.ToBitmap();
            if (myForm.ShowDialog() == DialogResult.OK)
                myForm.Dispose();
        }

        private void bgLbl_MouseHover(object sender, EventArgs e)
        {
            if (bgLbl.BorderStyle == BorderStyle.None)
                bgLbl.BorderStyle = BorderStyle.Fixed3D;
        }

        private void bgLbl_MouseLeave(object sender, EventArgs e)
        {
            if (bgLbl.BorderStyle == BorderStyle.Fixed3D)
                bgLbl.BorderStyle = BorderStyle.None;
        }

        private void exportColorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = saveFileDialog1.FileName;
                System.IO.StreamWriter myFile = new System.IO.StreamWriter(filename);
                myFile.WriteLine("Saved Colors");
                myFile.WriteLine("##############################");
                myFile.WriteLine("background-color: #" + bgColor.color.ToArgb().ToString("X8").Substring(2, 6));
                myFile.WriteLine("color: #" + fgColor.color.ToArgb().ToString("X8").Substring(2, 6));
                myFile.Close();
            }
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            Color myColor;
            if (comboBox1.SelectedIndex > 0 && comboBox1.SelectedIndex < colors.Length - 1)
                myColor = colors[comboBox1.SelectedIndex];
            else
                myColor = Color.Black;
            int r;
            int b;
            int g;
            r = (int)myColor.R;
            g = (int)myColor.G;
            b = (int)myColor.B;
            colorBtn.BackColor = myColor;
            textBox1.Text = "#" + myColor.ToArgb().ToString("X8").Substring(2, 6);
            redText.Text = r.ToString();
            greenText.Text = g.ToString();
            blueText.Text = b.ToString();
            colorDialog1.Color = myColor;
            colorSet = true;
            preset = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            splitContainer1.Dispose();
        }

        private void menuItem11_Click(object sender, EventArgs e)
        {
            PreviewBox otherFrm = new PreviewBox();
            otherFrm.splitContainer1.Panel2.BackColor = bgColor.color;
            otherFrm.label1.ForeColor = fgColor.color;
            otherFrm.label1.BackColor = bgColor.color;
            otherFrm.label2.ForeColor = fgColor.color;
            otherFrm.label2.BackColor = bgColor.color;
            otherFrm.bgLbl.Text = "#" + bgColor.color.ToArgb().ToString("X8").Substring(2, 6);
            otherFrm.fgLbl.Text = "#" + fgColor.color.ToArgb().ToString("X8").Substring(2, 6);
            otherFrm.splitContainer1.Panel2Collapsed = false;
            otherFrm.ShowDialog();
            otherFrm.Dispose();
        }
    }
}
